package com.mjf.cjt.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mjf.cjt.domain.TFcontactUnit;
import com.mjf.cjt.mapper.TFcontactUnitMapper;
import com.mjf.cjt.service.AsyncTMService;
import com.mjf.cjt.service.TFcontactUnitService;
import com.mjf.cjt.vo.PartnerVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * desc
 * </p>
 *
 * @author chenshoumeng
 * @date 2025/5/18 05:27
 */
@Service
public class TFcontactUnitServiceImpl extends ServiceImpl<TFcontactUnitMapper, TFcontactUnit> implements TFcontactUnitService {

    @Autowired
    private AsyncTMService asyncTMService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void asyncWLDW() {
        List<PartnerVO.InfoData> partnerVOS = asyncTMService.asyncWLDW();
        if (CollectionUtil.isEmpty(partnerVOS)) {
            return;
        }
        List<TFcontactUnit> units = new ArrayList<>();
        for (PartnerVO.InfoData partnerVO : partnerVOS) {
            TFcontactUnit tfcontactUnit = new TFcontactUnit();
            tfcontactUnit.setId(partnerVO.getId());
            tfcontactUnit.setCuid(String.valueOf(partnerVO.getId()));
            tfcontactUnit.setCucode(partnerVO.getCode());
            tfcontactUnit.setCuname(partnerVO.getName());
            tfcontactUnit.setShortHand(partnerVO.getShortHand());
            tfcontactUnit.setPartnerAbbName(partnerVO.getPartnerAbbName());
            tfcontactUnit.setCutypeId(String.valueOf(partnerVO.getPartnerType().getId()));
            tfcontactUnit.setCutypeName(partnerVO.getPartnerType().getName());
            tfcontactUnit.setCutypeCode(partnerVO.getPartnerType().getCode());
            tfcontactUnit.setPartnerClassId(String.valueOf(partnerVO.getPartnerClass().getId()));
            tfcontactUnit.setPartnerClassName(partnerVO.getPartnerClass().getName());
            tfcontactUnit.setPartnerClassCode(partnerVO.getPartnerClass().getCode());
            units.add(tfcontactUnit);
        }
        saveOrUpdateBatch(units, 500);
    }
}
